<html lang="en-US">

<head>
    
<title>Go Slice 最大容量大小是怎么来的 - 花落雨忧</title>

<meta property="og:title" content="Go Slice 最大容量大小是怎么来的 - 花落雨忧">



    



    
    <meta property="description" content="
[&amp;hellip;] 在《深入理解 Go Slice》中，我们提到了 “根据其类型大小去获取能够申请的最大容量大小” 的处理逻辑。今天我们将更深入地去探究一下，底层到底做了什么东西，涉及什么知识点？
Go Slice 对应代码如下：
[&amp;hellip;] func makeslice(et *_type, len, cap int) slice { maxElements := &amp;hellip;">
    <meta property="og:description" content="
[&amp;hellip;] 在《深入理解 Go Slice》中，我们提到了 “根据其类型大小去获取能够申请的最大容量大小” 的处理逻辑。今天我们将更深入地去探究一下，底层到底做了什么东西，涉及什么知识点？
Go Slice 对应代码如下：
[&amp;hellip;] func makeslice(et *_type, len, cap int) slice { maxElements := &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.asia/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.asia/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.asia/js/jquery.min.js"></script>




<link href="https://www.lican.asia/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>

        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/posts/go/slice/2019-01-06-why-slice-max/">Go Slice 最大容量大小是怎么来的</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2019/01/06</a>
                                            
                                            
                                            
                                            By 
                                            <a class="author" href="javascript:;">煎鱼</a></div>
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <p><img src="https://s2.ax1x.com/2020/02/27/3wnHRU.png" alt="image"></p>
<h2 id="前言">前言</h2>
<p>在《深入理解 Go Slice》中，我们提到了 “根据其类型大小去获取能够申请的最大容量大小” 的处理逻辑。今天我们将更深入地去探究一下，底层到底做了什么东西，涉及什么知识点？</p>
<p>Go Slice 对应代码如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">makeslice</span>(<span style="color:#a6e22e">et</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">_type</span>, <span style="color:#a6e22e">len</span>, <span style="color:#a6e22e">cap</span> <span style="color:#66d9ef">int</span>) <span style="color:#a6e22e">slice</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxElements</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">maxSliceCap</span>(<span style="color:#a6e22e">et</span>.<span style="color:#a6e22e">size</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">len</span> &lt; <span style="color:#ae81ff">0</span> <span style="color:#f92672">||</span> uintptr(<span style="color:#a6e22e">len</span>) &gt; <span style="color:#a6e22e">maxElements</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">cap</span> &lt; <span style="color:#a6e22e">len</span> <span style="color:#f92672">||</span> uintptr(<span style="color:#a6e22e">cap</span>) &gt; <span style="color:#a6e22e">maxElements</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">p</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">mallocgc</span>(<span style="color:#a6e22e">et</span>.<span style="color:#a6e22e">size</span><span style="color:#f92672">*</span>uintptr(<span style="color:#a6e22e">cap</span>), <span style="color:#a6e22e">et</span>, <span style="color:#66d9ef">true</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">slice</span>{<span style="color:#a6e22e">p</span>, <span style="color:#a6e22e">len</span>, <span style="color:#a6e22e">cap</span>}
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>根据想要追寻的逻辑，定位到了 <code>maxSliceCap</code> 方法，它会根据<strong>当前类型的大小获取到了所允许的最大容量大小</strong>来进行阈值判断，也就是安全检查。这是浅层的了解，我们继续追下去看看还做了些什么？</p>
<h2 id="maxslicecap">maxSliceCap</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">maxSliceCap</span>(<span style="color:#a6e22e">elemsize</span> <span style="color:#66d9ef">uintptr</span>) <span style="color:#66d9ef">uintptr</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">elemsize</span> &lt; uintptr(len(<span style="color:#a6e22e">maxElems</span>)) {
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">maxElems</span>[<span style="color:#a6e22e">elemsize</span>]
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#a6e22e">elemsize</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="maxelems">maxElems</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">maxElems</span> = [<span style="color:#f92672">...</span>]<span style="color:#66d9ef">uintptr</span>{
</span></span><span style="display:flex;"><span>	^uintptr(<span style="color:#ae81ff">0</span>),
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">1</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">2</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">3</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">4</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">5</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">6</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">7</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">8</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">9</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">10</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">11</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">12</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">13</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">14</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">15</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">16</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">17</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">18</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">19</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">20</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">21</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">22</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">23</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">24</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">25</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">26</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">27</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">28</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">29</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">30</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">31</span>, <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">32</span>,
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><code>maxElems</code> 是包含一些预定义的切片最大容量值的查找表，索引是切片元素的类型大小。而值看起来 “奇奇怪怪” 不大眼熟，都是些什么呢。主要是以下三个核心点：</p>
<ul>
<li>^uintptr(0)</li>
<li>maxAlloc</li>
<li>maxAlloc / typeSize</li>
</ul>
<h3 id="uintptr0">^uintptr(0)</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;uintptr: %v\n&#34;</span>, uintptr(<span style="color:#ae81ff">0</span>))
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;^uintptr: %v\n&#34;</span>, ^uintptr(<span style="color:#ae81ff">0</span>))
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>输出结果：</p>
<pre tabindex="0"><code>2019/01/05 17:51:52 uintptr: 0
2019/01/05 17:51:52 ^uintptr: 18446744073709551615
</code></pre><p>我们留意一下输出结果，比较神奇。取反之后为什么是 18446744073709551615 呢？</p>
<h3 id="uintptr-是什么">uintptr 是什么</h3>
<p>在分析之前，我们要知道 uintptr 的本质（真面目），也就是它的类型是什么，如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#66d9ef">uintptr</span> <span style="color:#66d9ef">uintptr</span>
</span></span></code></pre></div><p>uintptr 的类型是自定义类型，接着找它的真面目，如下：</p>
<pre tabindex="0"><code>#ifdef _64BIT
typedef	uint64		uintptr;
#else
typedef	uint32		uintptr;
#endif
</code></pre><p>通过对以上代码的分析，可得出以下结论：</p>
<ul>
<li>在 32 位系统下，uintptr 为 uint32 类型，占用大小为 4 个字节</li>
<li>在 64 位系统下，uintptr 为 uint64 类型，占用大小为 8 个字节</li>
</ul>
<h3 id="uintptr-做了什么事">^uintptr 做了什么事</h3>
<p>^ 位运算符的作用是<strong>按位异或</strong>，如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Println</span>(^<span style="color:#ae81ff">1</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Println</span>(^uint64(<span style="color:#ae81ff">0</span>))
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>输出结果：</p>
<pre tabindex="0"><code>2019/01/05 20:44:49 -2
2019/01/05 20:44:49 18446744073709551615
</code></pre><p>接下来我们分析一下，这两段代码都做了什么事情呢</p>
<h4 id="1">^1</h4>
<p>二进制：0001</p>
<p>按位取反：1110</p>
<p>该数为有符号整数，最高位为符号位。低三位为表示数值。按位取反后为 1110，根据先前的说明，最高位为 1，因此表示为 -。取反后 110 对应十进制 -2</p>
<h4 id="uint640">^uint64(0)</h4>
<p>二进制：0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000</p>
<p>按位取反：1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111</p>
<p>该数为无符号整数，该位取反后得到十进制值为：18446744073709551615</p>
<p>这个值是不是看起来很眼熟呢？没错，就是 <code>^uintptr(0)</code> 的值。也印证了其底层数据类型为 uint64 的事实 （本机为 64 位）。同时它又代表如下：</p>
<ul>
<li>math.MaxUint64</li>
<li>2 的 64 次方减 1</li>
</ul>
<h3 id="maxalloc">maxAlloc</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">GoarchMips</span> = <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">GoarchMipsle</span> = <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">GoarchWasm</span> = <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">_64bit</span> = <span style="color:#ae81ff">1</span> <span style="color:#f92672">&lt;&lt;</span> (^uintptr(<span style="color:#ae81ff">0</span>) <span style="color:#f92672">&gt;&gt;</span> <span style="color:#ae81ff">63</span>) <span style="color:#f92672">/</span> <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">heapAddrBits</span> = (<span style="color:#a6e22e">_64bit</span><span style="color:#f92672">*</span>(<span style="color:#ae81ff">1</span><span style="color:#f92672">-</span><span style="color:#a6e22e">sys</span>.<span style="color:#a6e22e">GoarchWasm</span>))<span style="color:#f92672">*</span><span style="color:#ae81ff">48</span> <span style="color:#f92672">+</span> (<span style="color:#ae81ff">1</span><span style="color:#f92672">-</span><span style="color:#a6e22e">_64bit</span><span style="color:#f92672">+</span><span style="color:#a6e22e">sys</span>.<span style="color:#a6e22e">GoarchWasm</span>)<span style="color:#f92672">*</span>(<span style="color:#ae81ff">32</span><span style="color:#f92672">-</span>(<span style="color:#a6e22e">sys</span>.<span style="color:#a6e22e">GoarchMips</span><span style="color:#f92672">+</span><span style="color:#a6e22e">sys</span>.<span style="color:#a6e22e">GoarchMipsle</span>))
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">maxAlloc</span> = (<span style="color:#ae81ff">1</span> <span style="color:#f92672">&lt;&lt;</span> <span style="color:#a6e22e">heapAddrBits</span>) <span style="color:#f92672">-</span> (<span style="color:#ae81ff">1</span><span style="color:#f92672">-</span><span style="color:#a6e22e">_64bit</span>)<span style="color:#f92672">*</span><span style="color:#ae81ff">1</span>
</span></span></code></pre></div><p><code>maxAlloc</code> 是<strong>允许用户分配的最大虚拟内存空间</strong>。在 64 位，理论上可分配最大 <code>1 &lt;&lt; heapAddrBits</code> 字节。在 32 位，最大可分配小于 <code>1 &lt;&lt; 32</code> 字节</p>
<p>在本文，仅需了解它承载的是什么就好了。具体的在以后内存管理的文章再讲述</p>
<p>注：该变量在 go 10.1 为 <code>_MaxMem</code>，go 11.4 已改为 <code>maxAlloc</code>。相关的 <code>heapAddrBits</code> 计算方式也有所改变</p>
<h3 id="maxalloc--typesize">maxAlloc / typeSize</h3>
<p>我们再次回顾 <code>maxSliceCap</code> 的逻辑代码，这次重点放在控制逻辑，如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#75715e">// func makeslice
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">maxElements</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">maxSliceCap</span>(<span style="color:#a6e22e">et</span>.<span style="color:#a6e22e">size</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// func maxSliceCap
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">if</span> <span style="color:#a6e22e">elemsize</span> &lt; uintptr(len(<span style="color:#a6e22e">maxElems</span>)) {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">maxElems</span>[<span style="color:#a6e22e">elemsize</span>]
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">return</span> <span style="color:#a6e22e">maxAlloc</span> <span style="color:#f92672">/</span> <span style="color:#a6e22e">elemsize</span>
</span></span></code></pre></div><p>通过这段代码和 Slice 上下文逻辑，可得知在想得到该类型的最大容量大小时。会根据对应的类型大小去查找表查找索引（索引为类型大小，摆放顺序是有考虑原因的）。“迫不得已的情况下” 才会手动的计算它的值，最终计算得到的内存字节大小都为该类型大小的整数倍</p>
<p>查找表的设置，更像是一个优化逻辑。减少常用的计算开销 :)</p>
<h2 id="总结">总结</h2>
<p>通过本文的分析，可得出 Slice 所允许申请的最大容量大小，与当前<strong>值类型</strong>和当前<strong>平台位数</strong>有直接关系</p>
<h2 id="最后">最后</h2>
<p>本文与<a href="https://github.com/EDDYCJY/blog/blob/master/golang/pkg/2018-12-15-%E6%9C%89%E7%82%B9%E4%B8%8D%E5%AE%89%E5%85%A8%E5%8D%B4%E5%8F%88%E4%B8%80%E4%BA%AE%E7%9A%84Go-unsafe-Pointer.md">《有点不安全却又一亮的 Go unsafe.Pointer》</a>一同属于<a href="https://github.com/EDDYCJY/blog/blob/master/golang/pkg/2018-12-11-%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3Go-Slice.md">《深入理解 Go Slice》</a>的关联章节。如果你在阅读源码时，对这些片段有疑惑。记得想尽办法深究下去，搞懂它</p>
<p>短短的一句话其实蕴含着不少知识点，希望这篇文章恰恰好可以帮你解惑</p>
<p>注：本文 Go 代码基于版本 11.4</p>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/pkg/2018-12-15-unsafe/">有点不安全却又一亮的 Go unsafe.Pointer</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/slice/2018-12-11-slice/">深入理解 Go Slice</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/pkg/2018-12-04-fmt/">fmt 标准库 --- Print* 是怎么样输出的？</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/pkg/2018-09-28-log/">log 标准库</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/talk/2018-12-26-go-memory-align/">在 Go 中恰到好处的内存对齐</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            <li>
                                                <a href="/tags/%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90" title="View all posts tagged match">源码分析</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.asia/posts/posts/go/talk/2018-12-26-go-memory-align/"
                                                    rel="prev">在 Go 中恰到好处的内存对齐</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.asia/posts/posts/go/talk/2019-01-20-control-goroutine/">来，控制一下 goroutine 的并发数量</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.asia/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    <section id="qrcode" class="widget widget_media_image">
        <h2 class="widget-title">微信公众号</h2>
        <img width="258" height="258"
            src="https://www.lican.asia/wechat/lican.png"
            class="image wp-image-5514  attachment-full size-full" alt=""
            style="max-width: 100%; height: auto;"
            sizes="(max-width: 258px) 100vw, 258px">
    </section>
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.asia All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>